<?php
include "config_shop.inc.php";
$Subscribes = SubscribeManager::getInstance()->getAll("status = 'notsent'");
foreach ($Subscribes as $Subscribe){
	$Subscribe->status = 'inprogress';
	$Subscribe->save();
	$SubscribeTemplate = SubscribeTemplatesManager::getInstance()->getByPk($Subscribe->template_id);
	$GroupIds = explode(',', $Subscribe->group_id);
	foreach ($GroupIds as $gid){
		$Subscribers = SubscribersManager::getInstance()->getAll("`group_id` like '%$gid%'");
		foreach ($Subscribers as $Subscriber){
			$mail = new html_mime_mail();
			$letter = "
                <html>
                <body>
                	Шановний (-а), {$Subscriber->full_name}!<p>
                	{$SubscribeTemplate->content}
					<p>Команда <a href='http://{$_SERVER["SERVER_NAME"]}/'>Крамниця-А</a>.
                </body>
                </html>";
			$mail->add_html($letter);
			$mail->build_message("w");
			if ($mail->SendMail("noreply@kramnyca.info",'Крамниця - А  |  Едине місце актуальних українських товарів',$Subscriber->email,$Subscriber->full_name, $Subscribe->title)) 
			echo "OK";
			else echo "OOPS";
			sleep(3.5);
		}
	}
	$Subscribe->status = 'sent';
	$Subscribe->save();
}
echo "Subscribes sent";